package com.e.h.sheep.etalk;
//
//import java.io.File;
//
//import org.jivesoftware.smack.Connection;
//import org.jivesoftware.smack.ConnectionConfiguration;
//import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
//import org.jivesoftware.smack.SmackConfiguration;
//import org.jivesoftware.smack.XMPPConnection;
//import org.jivesoftware.smack.XMPPException;
//
//import android.app.Activity;
//import android.content.Intent;
//import android.content.SharedPreferences;
//import android.os.Bundle;
//import android.os.Handler;
//import android.preference.PreferenceManager;
//import android.util.Log;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.jivesoftware.smack.Roster;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.EditText;
import android.accounts.Account;
import android.accounts.AccountManager;

public class MainActivity extends Activity implements OnClickListener{

	private static final int MODE_PRIVATEE = 0;
	private Button account, connect;
	private EditText idText;
	
//	private String id,pass;
//	private  final String GTALK_HOST = "talk.google.com";
//	private  final int GTALK_PORT = 5222;
//	private  final String GTALK_SERVICE = "gmail.com";
//	private  EditText editText1;
//	private  TextView send, receive;
//	private  Handler handler;
////	public String msg_body;
//
//	ConnectionConfiguration connectionConfiguration;
//	XMPPConnection xmppCon;

	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_main);
		account = (Button)findViewById(R.id.accountSet);
		connect = (Button)findViewById(R.id.connect);
		idText =  (EditText)findViewById(R.id.toAddress);
		
		// デフォルトのプリファレンスを生成
		SharedPreferences pref = getPreferences(MODE_PRIVATE);
		// プリファレンスからデータを取得する
		 idText.setText(pref.getString("toAddress","@gmail.com"));
		
		
	}

	@Override
	public void onClick(View v) {
		if(v == account){
			Log.d("log", "accountset");
			try {
				String[] getAcunt = getAccounts();
			} catch (AuthenticatorException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
	
			Intent intent = new Intent(this, AccountSet.class);
			this.startActivity(intent);
		}

		if(v == connect){
				
			String idAdr = idText.getText().toString();
			SharedPreferences pref = getPreferences(MODE_PRIVATE);
			// 書き込み用のEditorオブジェクトを取得
			SharedPreferences.Editor edit = pref.edit();
			// 値の書き込み
			edit.putString("toAddress",idAdr);
			// 書き込み内容の確定
			edit.commit();

				Intent intent = new Intent(this, Gchat.class);
				intent.putExtra("toAddress",idAdr);
				this.startActivity(intent);

		}
	}
	
	 public String[] getAccounts() throws AuthenticatorException, IOException {  
	        ArrayList<String> accountsInfo = new ArrayList<String>();
	        Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");
	        for (Account account : accounts) {  
	            String name = account.name;  
	            String type = account.type;  
	            String authToken;
	            int describeContents = account.describeContents();  
	            int hashCode = account.hashCode();  
	            AccountManager accountManager = AccountManager.get(getApplicationContext());  
	            AccountManagerFuture<Bundle> accountManagerFuture =   
	            		accountManager.getAuthToken(account, "mail", null, this, null, null);  
		            Bundle authTokenBundle;  
		            try {  
		                authTokenBundle = accountManagerFuture.getResult();  
		                authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();  
		                Log.i("TAG","authToken:"+authToken); 
		                
		            } catch (OperationCanceledException e) {  
		                // TODO Auto-generated catch block  
		                e.printStackTrace();  
		            } catch (AuthenticatorException e) {  
		                // TODO Auto-generated catch block  
		                e.printStackTrace();  
		            } catch (IOException e) {  
		                // TODO Auto-generated catch block  
		                e.printStackTrace();  
		            }  
				
				

	            	accountsInfo.add("name = " + name +   
	            			"\ntype = " + type +   
	            			"\ndescribeContents = " + describeContents +   
	            			"\nhashCode = " + hashCode);
	            
	        }  

	        String[] result = new String[accountsInfo.size()];  
	        accountsInfo.toArray(result);  
	        return result;  
	    }  
	 
	 
	 


}





